package com.fzy.silntgym.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fzy.silntgym.R;
import com.fzy.silntgym.application.UserAgent;


public class LoginActivity extends AppCompatActivity {
    Button buttonin,buttonup;
    EditText user,pass;
    String username = null;
    String password = null;
    private long exitTime=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitleTextColor(0xFFFFFFFF);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        buttonin = (Button)findViewById(R.id.signin);
        buttonup = (Button)findViewById(R.id.signup);

        user = (EditText)findViewById(R.id.username) ;
        pass = (EditText)findViewById(R.id.password) ;
        buttonin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = user.getText().toString();
                password = pass.getText().toString();
                if(username.isEmpty()
                        ||password.isEmpty()){
                    Toast.makeText(getApplicationContext(),"用户名或密码不能为空",Toast.LENGTH_LONG).show();
                    UserAgent.islogin = false;
                }
                else if(username.equals(UserAgent.username)
                        &&password.equals(UserAgent.password)){
                    Toast.makeText(getApplicationContext(),"欢迎回来！即将跳转回主界面",Toast.LENGTH_LONG).show();
                    UserAgent.islogin = true;
                    finish();
                }
                else {
                    Toast.makeText(getApplicationContext(),"账号或者密码错误",Toast.LENGTH_LONG).show();
                    UserAgent.islogin = false;
                }
            }
        });
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        buttonup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),RegisterActivity.class);
                startActivity(intent);

            }
        });

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 3000) {
                Toast.makeText(getApplicationContext(), "再按一次返回",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
